package com.miguo.utils;

import java.io.File;

import android.os.Handler;
import android.os.Message;

import com.miguo.app.AppContext;
import com.miguo.framework.utils.Toast;

public class ClearCache {

    /**
     * 清除app缓存
     */
    public static void App() {
        final Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    Toast.show(AppContext.getContext(), "清除成功");
                } else {
                    Toast.show(AppContext.getContext(), "缓存清除失败");
                }
            }
        };
        new Thread() {
            public void run() {
                Message msg = new Message();
                try {
                    Clear();
                    msg.what = 1;
                } catch (Exception e) {
                    e.printStackTrace();
                    msg.what = -1;
                }
                handler.sendMessage(msg);
            }
        }.start();
    }


    /**
     * 清除app缓存
     */
    public static void Clear() {
        // 清除WebView缓存
        ClearWebViewCache();
        // 清除数据缓存
        ClearCacheFolder(AppContext.getContext().getFilesDir(), System.currentTimeMillis());
        ClearCacheFolder(AppContext.getContext().getCacheDir(), System.currentTimeMillis());
        // 2.2版本才有将应用缓存转移到sd卡的功能
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
            ClearCacheFolder(AppContext.getContext().getExternalCacheDir(),
                    System.currentTimeMillis());
        }
    }

    /**
     * 清除WebView缓存
     */
    private static void ClearWebViewCache() {

        // 清理Webview缓存数据库
        try {
            AppContext.getContext().deleteDatabase("webview.db");
            AppContext.getContext().deleteDatabase("webview.db-shm");
            AppContext.getContext().deleteDatabase("webview.db-wal");
            AppContext.getContext().deleteDatabase("webviewCache.db");
            AppContext.getContext().deleteDatabase("webviewCache.db-shm");
            AppContext.getContext().deleteDatabase("webviewCache.db-wal");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除缓存目录
     *
     * @param dir     目录
     * @param curTime 当前系统时间
     * @return
     */
    private static int ClearCacheFolder(File dir, long curTime) {
        int deletedFiles = 0;
        if (dir != null && dir.isDirectory()) {
            try {
                for (File child : dir.listFiles()) {
                    if (child.isDirectory()) {
                        deletedFiles += ClearCacheFolder(child, curTime);
                    }
                    if (child.lastModified() < curTime) {
                        if (child.delete()) {
                            deletedFiles++;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return deletedFiles;
    }

}
